<?php
require_once(ABSPATH .'include/dao/UserDAO.php');
require_once(ABSPATH .'include/dao/CategoryDAO.php');

function set_current_user($usr){
	$_SESSION["current_usr"]= serialize($usr);
	$hash = $usr->access_token_secret;
	setcookie('siteAuth',"usr=$usr->id&hash=$hash",time()+ (3600 * 24 * 30));
}

function getCurrentUser(){
	if($_SESSION["current_usr"]){
		return 	unserialize($_SESSION["current_usr"]);
	}elseif($_COOKIE["siteAuth"]){		
		parse_str($_COOKIE["siteAuth"],$authinfo);			
		$usrid = $authinfo["usr"];
		$hashcode = $authinfo["hash"];
		
		$dao=new UserDAO();
		$usr = $dao->get($usrid);		
			
		if($usr->access_token_secret== $hashcode){
			set_current_user($usr);			
			return $usr;
		}
			
		return false;
			
	}else{		
		return false;
	}
}

function getCategories(){			
	$mkey = "mckey_Categories";
	$categories = false;	
	$mmc=@memcache_init();	
	if($mmc){
		$categories = unserialize(memcache_get($mmc,$mkey));		
		if(!$categories){
			$dao = new categoriesDAO();	
			$categories = $dao-> get_all();
			memcache_set($mmc,$mkey,serialize($categories),0, 30);
		}
	}else{
		$dao = new categoriesDAO();	
		$categories = $dao-> get_all();
	}	
	return $categories;
}

function getCategory($name){
	$categories = getCategories();
	if(!$categories) return false;
	foreach ($categories as $cat){
		if($cat->name == $name)
			return $cat;
	}
	return false;
}
?>